﻿using RMS.BLL;
using RMS.Common;
using RMS.EF.Model;
using RMS.EF.Model.Common;
using RMS.Manage.Web.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RMS.Manage.Web.Areas.RMS.Controllers
{
    [RMSAuthorize]
    public class ProgramController : BaseController
    {

        ProgramBll bll = new ProgramBll();
        /// <summary>
        /// 列表页
        /// </summary>
        /// <returns></returns>
        public ActionResult List()
        {
            return View();
        }
        /// <summary>
        /// 列表分页查询
        /// </summary>
        /// <param name="searchValue">搜索内容</param>
        /// <param name="status">状态</param>
        /// <returns></returns>
        public ActionResult ListGetData(string searchValue, E_Status status)
        {
            PageParams page = SetPageParams();
            var data = bll.GetByPage(page, searchValue, status);
            var result = SetPageJson(page, data);
            return Json(result);
        }
        /// <summary>
        /// 删除和批量删除
        /// </summary>
        /// <param name="id">Id字符串","分隔</param>
        /// <returns></returns>
        public ActionResult Delete(string id)
        {
            CommonOutPutVm result = new CommonOutPutVm();
            try
            {
                if (string.IsNullOrWhiteSpace(id))
                {
                    throw new Exception("Please select the data");
                }
                result = bll.Delete(id);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }
            return Json(result);
        }
        /// <summary>
        /// 编辑页
        /// </summary>
        /// <param name="id">数据Id</param>
        /// <returns></returns>
        public ActionResult Edit(string id)
        {
            RMS_Program_Extend model = new RMS_Program_Extend();
            if (!string.IsNullOrWhiteSpace(id))
            {
                var m = bll.GetById(id);
                model = ToolsHelper.ConverToTEntity<RMS_Program_Extend>(m);
            }
            return PartialView(model);
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="model">编辑后的实体</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(RMS_Program_Extend model_e)
        {
            CommonOutPutVm result = new CommonOutPutVm();
            try
            {
                var errmsg = ModelState.CheckHtmlErrorMsg();
                if (!string.IsNullOrWhiteSpace(errmsg))
                {
                    result.Message = errmsg;
                    return Json(result);
                }
                #region 模型赋值
                if (string.IsNullOrWhiteSpace(model_e.Id))
                {
                    model_e.LastUpdateTime = DateTime.Now;
                    model_e.LastUpdateUser = UserId;
                }
                else
                {
                    model_e.EDeleted = E_Deleted.Normal;
                    model_e.CreationTime = DateTime.Now;
                    model_e.CreationUser = UserId;
                    model_e.LastUpdateTime = DateTime.Now;
                    model_e.LastUpdateUser = UserId;
                }
                #endregion
                RMS_Program model = ToolsHelper.ConverToTEntity<RMS_Program>(model_e);
                result = bll.Edit(model);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }
            return Json(result);

        }
    }
}